const fs = require("fs");
const vm = require("vm");
const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default
types = require("@babel/types")
generate = require("@babel/generator").default

// const {ResourceLoader, WindowManager, randomNum} = require("../../../window");

// scope相关:
// path.scope 获取当前路径的作用域
// scope.dump() 打印当前作用域
//
// scope.rename(oldName, newName, block)  变量重命名，会修改所有的变量绑定
// scope.getBinding(name) 获取当前所有绑定
// scope.getBinding(name).referenced 绑定是否被引用
// scope.getBinding(name).constantViolations 获取当前所有绑定修改
// scope.getBinding(name).referencePaths  获取当前所有绑定路径

function n2c(node) {
    const jsCode = generate(node, {minified: true}).code;
    // console.log(jsCode);
    return jsCode;
}

global.n2c = n2c;
function body2codeString(body) {
    const jsCode = n2c(types.BlockStatement(body));
    return jsCode.substring(1, jsCode.length-1);
}
global.body2codeString = body2codeString;

let jscode = fs.readFileSync('/Users/cuijun/work/gitee/OptimizeJs/sample/a.js').toString();

var ast = parser.parse(jscode);

const body1StringRestitute = {
    VariableDeclarator(path) {
        const {scrop,node} = path;
        console.log(node);
        if (types.isVariableDeclaration(node)){
            console.log(node.declarations);
        }
        // console.log(scrop);
        // console.log(node);
    }
}

traverse(ast, body1StringRestitute);

// ast = parser.parse(n2c(ast));

const result = n2c(ast);
fs.writeFileSync('/Users/cuijun/work/gitee/OptimizeJs/sample/a_dec.js', result);
